package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusSubject;
import com.meida.module.bus.client.enums.PayStateEnum;
import com.meida.module.bus.client.enums.PayTypeEnum;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.module.bus.provider.service.BusSubjectService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 后台订单列表
 */
@Component
public class BusOrderPageListHandler implements PageInterceptor<BusOrder> {

    @Autowired
    private BusOrderService busOrderService;

    @Autowired
    private BusSubjectService busSubjectService;

    @Override
    public void prepare(CriteriaQuery<BusOrder> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String userContent = params.get("userContent");
        String answerUserContent = params.get("answerUserContent");
        String endDate = params.get("endDate");
        cq.select(BusOrder.class, "orderId", "coverImg", "orderNo", "orderAmount", "discountAmount", "rewardAmount", "payableAmount", "commission", "orderType", "subjectIds", "orderState", "payState", "payType", "createTime");
        cq.select(BusSubject.class, "subjectTitle");
        cq.select(AppUser.class, "nickName", "userNo", "mobile");
        cq.select("scholarTyrantUser.nickName scholarTyrantUserNickName", "scholarTyrantUser.userNo scholarTyrantUserNo");
        cq.eq(BusOrder.class, "orderType");
        cq.eq(BusOrder.class, "orderState");
        cq.eq(BusOrder.class, "userId");
        cq.eq(BusOrder.class, "payState", PayStateEnum.HavePay.getValue());
        if (FlymeUtils.isNotEmpty(userContent)) {
            cq.and(true, q -> {
                q.like("user.nickName", userContent);
                q.or().like("user.userNo", userContent);
                q.or().like("user.mobile", userContent);
            });
        }
        if (FlymeUtils.isNotEmpty(answerUserContent)) {
            cq.and(true, q -> {
                q.like("scholarTyrantUser.nickName", answerUserContent);
                q.or().like("scholarTyrantUser.userNo", answerUserContent);
            });
        }
        cq.like(BusOrder.class, "orderNo");
        cq.ge("DATE_FORMAT(busOrder.createTime,'%Y-%m-%d')", beginDate);
        cq.le("DATE_FORMAT(busOrder.createTime,'%Y-%m-%d')", endDate);
        cq.createJoin(AppUser.class).setMainField("userId").setJoinField("userId");
        cq.createJoin(AppUser.class).setJoinAlias("scholarTyrantUser").setMainField("answerUserId").setJoinField("userId");
        cq.createJoin(BusSubject.class);
    }

    @Override
    public void complete(CriteriaQuery<BusOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String subjectIds = entityMap.get("subjectIds");
            List<String> subjectTitles = busSubjectService.selectSubjectTitles(subjectIds);
            entityMap.put("subjectTitles", subjectTitles);
            String orderState = entityMap.get("orderState");
            //订单类型和状态转换
            Integer orderType = entityMap.getInt("orderType");
            Map orderStateMap = busOrderService.getOrderStateDesc(orderType, orderState);
            entityMap.putAll(orderStateMap);
            String payType = entityMap.get("payType");
            PayTypeEnum payTypeEnum = PayTypeEnum.getByValue(payType);
            if (FlymeUtils.isNotEmpty(payTypeEnum)) {
                entityMap.put("payTypeText", payTypeEnum.getLabel());
            }
        }
    }

}
